package com.example.app.components.MyTextField

import android.view.Window
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Cancel
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.OutlinedTextFieldDefaults.colors
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextIndent
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.app.components.MyButton.MyTextButton
import com.example.app.components.MyIcon.MyIcon
import com.example.app.components.MySpacer.MySpacer
import com.example.app.components.MyText.MyText
import com.example.app.ui.theme.listItemStartPadding
import com.example.app.ui.theme.my_description_color
import com.google.accompanist.insets.navigationBarsWithImePadding
import com.google.accompanist.insets.statusBarsPadding

@Composable
fun MySearchTextField(
        props: @Composable (@Composable (value: TextFieldValue) -> Unit) -> Unit,
        emitsFromMySearchTextField: (value: TextFieldValue) -> Unit,
        hasPadding: Boolean? = true,
        showCancelBtn: Boolean? = true
) {
    props({
        Row(
            horizontalArrangement = Arrangement.Start,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            if (hasPadding!!) {
                MySpacer(modifier = Modifier.width(listItemStartPadding))
            }
            Row(
                horizontalArrangement = Arrangement.Start,
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .clip(MaterialTheme.shapes.small)
                    .weight(1f)
                    .height(OutlinedTextFieldDefaults.MinHeight)
                    .background(MaterialTheme.colorScheme.background)
            ) {
                MySpacer(modifier = Modifier.width(listItemStartPadding))
                MyIcon(
                    imageVector = Icons.Rounded.Search,
                    tint = my_description_color
                )

                MyOutlinedTextField(
                    textStyle = LocalTextStyle.current.copy(
                        textIndent = TextIndent(0.sp),
                        fontSize = 14.sp,
                        lineHeight = with(LocalDensity.current) {
                            OutlinedTextFieldDefaults.MinHeight.value.toSp()
                        }),
                    singleLine = true,
                    modifier = Modifier
                        .fillMaxHeight()
                        .weight(1f),
                    textFieldValue = it,
                    textFieldValueChange = {
                        emitsFromMySearchTextField(it)
                    },
                    colors = colors(
                        disabledBorderColor = Color.Transparent,
                        errorBorderColor = Color.Transparent,
                        focusedBorderColor = Color.Transparent,
                        unfocusedBorderColor = Color.Transparent,
                    )
                )
                if (!it.text.isEmpty()) {
                    MyIcon(
                        imageVector = Icons.Rounded.Cancel,
                        tint = my_description_color,
                        modifier = Modifier.clickable {
                            emitsFromMySearchTextField(it.copy(text = ""))
                        },
                    )
                }
                MySpacer(modifier = Modifier.width(6.dp))
            }
            if (showCancelBtn!!) {
                MyTextButton(onClick = { /*TODO*/ }) {
                    MyText(text = "取消")
                }
            }
            if (hasPadding!!) {
                MySpacer(modifier = Modifier.width(listItemStartPadding))
            }
        }
    })
}
